package com.pagoda.nerp.trade.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pagoda.nerp.trade.common.PageListResult;
import com.pagoda.nerp.trade.dao.MdAccountRoleDao;
import com.pagoda.nerp.trade.entity.MdAccountRole;
import com.pagoda.nerp.trade.service.MdAccountRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Jungle on 2016/12/16.
 */
@Service
public class MdAccountRoleServiceImpl implements MdAccountRoleService {

    private static final Logger logger = LoggerFactory.getLogger(MdAccountRoleServiceImpl.class);

    @Autowired
    private MdAccountRoleDao mdAccountRoleDao;

    /**
     * 根据条件获取当前账号和角色列表的关系
     * @param map
     * @return
     */
    @Override
    public Object listMdAccountRoleByCon(Object map) throws Exception {
        if(!(map instanceof Map)){
            logger.error("请求参数为非MAP类型，转换错误！！");
            throw new Exception("请求参数为非Map类型");
        }
        Map con = (Map) map;
        Page<MdAccountRole> pageInfo = null;
        if(con.get("pageNo") != null){
            int pageNo = con.get("pageNo") != null ? Integer.parseInt(con.get("pageNo").toString()) : 1;
            int pageSize = con.get("pageSize") != null ?  Integer.parseInt(con.get("pageSize").toString()) : 0;
            pageInfo = PageHelper.startPage(pageNo, pageSize);
        }
        List<MdAccountRole> listAccount = (List<MdAccountRole>) mdAccountRoleDao.selectAllByConditon(con);
        return new PageListResult<MdAccountRole>(pageInfo,listAccount);
    }

    /**
     * 删除子账户与角色关系
     * @param condition
     * @return
     */
    @Transactional
    @Override
    public int deleteMdAccountRoleByCon(Object condition) {
        int delRes = mdAccountRoleDao.deleteByCondition((Map) condition);
        if(delRes <= 0){
            throw new RuntimeException("Service 解除账号与角色关系失败!!");
        }
        return delRes;
    }
}
